package logic

import (
	"context"
	"cp/cache"
	"cp/models/mysql"
	"errors"

	"cp/cprpc/cprpc"
	"cp/cprpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *cprpc.LoginRequest) (*cprpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	result, _ := cache.Rdb.Get(l.ctx, "sendCode"+in.Phone).Result()
	if result != in.SendCode {
		return nil, errors.New("短信验证码错误,请重新输入")
	}

	var user = mysql.User{}
	mysql.DB.Debug().Find(&user)

	if user.Status == "1" {
		return nil, errors.New("账号已禁用")
	}

	return &cprpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
